package com.mikgonz.farkle;

import java.util.HashMap;

import android.util.Log;
import android.view.View;

public class DieManager {

	public static final int INDEX_FLAG = 1;
	public static final int VALUE_FLAG = 2;
	public static final int ABS_VALUE_FLAG = 3;
	

	private int[] diceValues = { 1, 2, 3, 4, 5, 6 };
	private HashMap<Integer, Integer> diceImages = new HashMap<Integer, Integer>();
	private HashMap<Integer, Integer> letterImages = new HashMap<Integer, Integer>();

	// Sets @newValue to dieValues[@index]
	public void setValue(int index, int newValue) {
		Log.w(getClass().getName(), "Index = " + index + " Value = " + newValue);
		diceValues[index] = newValue;
	}

	public DieManager() {
		super();
		initializeMaps();
	}

	private void initializeMaps() {

		diceImages.put(-6, R.drawable.dice_6_glow);
		diceImages.put(-5, R.drawable.dice_5_glow);
		diceImages.put(-4, R.drawable.dice_4_glow);
		diceImages.put(-3, R.drawable.dice_3_glow);
		diceImages.put(-2, R.drawable.dice_2_glow);
		diceImages.put(-1, R.drawable.dice_1_glow);
		diceImages.put(1, R.drawable.dice_1);
		diceImages.put(2, R.drawable.dice_2);
		diceImages.put(3, R.drawable.dice_3);
		diceImages.put(4, R.drawable.dice_4);
		diceImages.put(5, R.drawable.dice_5);
		diceImages.put(6, R.drawable.dice_6);

		letterImages.put(0, R.drawable.f);
		letterImages.put(1, R.drawable.a);
		letterImages.put(2, R.drawable.r);
		letterImages.put(3, R.drawable.k);
		letterImages.put(4, R.drawable.l);
		letterImages.put(5, R.drawable.e);

	}

	public void rollDice() {

		boolean isNewRound = (numOnTable() == 0);

		for (int j = 0; j < 6; j++) {

			if (isNewRound || diceValues[j] != 0)
				diceValues[j] = (int) ((Math.random() * 6) + 1);

		}
	}

	public int getValue(int index, int flag) {
		if (flag == ABS_VALUE_FLAG)
			return Math.abs(diceValues[index]);

		return diceValues[index];
	}

	private int numOnTable() {
		int count = 6;
		for (int i : diceValues) {
			if (i == 0)
				count--;
		}
		return count;
	}

	public void pickUp(int[] highlighted) {

		for (int i = 0; i < highlighted.length; i++)
			diceValues[highlighted[i]] = 0;

	}

	public void toggleHighlight(int index) {
		diceValues[index] *= -1;
	}

	public void clearTable() {
		diceValues[0] = 0;
		diceValues[1] = 0;
		diceValues[2] = 0;
		diceValues[3] = 0;
		diceValues[4] = 0;
		diceValues[5] = 0;

	}

	public int[] diceOnTable(int flag) {
		if (flag == ABS_VALUE_FLAG) {
			int[] array = new int[diceValues.length];
			for (int i = 0; i < diceValues.length; i++)
				array[i] = Math.abs(diceValues[i]);
			return array;
		}

		return diceValues;
	}

	public int[] getHighlighted(int flag) {
		int[] dirtyArray = { 0, 0, 0, 0, 0, 0 };
		int count = 0;

		for (int j = 0; j < 6; j++) {
			if (diceValues[j] < 0) {

				if (flag == INDEX_FLAG)
					dirtyArray[count++] = j;

				if (flag == VALUE_FLAG)
					dirtyArray[count++] = diceValues[j];

				if (flag == ABS_VALUE_FLAG)
					dirtyArray[count++] = Math.abs(diceValues[j]);

			}
		}

		if (count == 0)
			return null;

		int[] cleanArray = new int[count];
		System.arraycopy(dirtyArray, 0, cleanArray, 0, count);
		return cleanArray;

	}

	// Finds in dieValues same @value and excludes @index
	public int[] findPairs(int index, int flag) {

		int[] dirtyArray = { 0, 0, 0, 0, 0, 0 };

		int count = 0;

		for (int j = 0; j < 6; j++) {

			if (j != index
					&& Math.abs(diceValues[j]) == Math.abs(diceValues[index])) {

				if (flag == INDEX_FLAG)
					dirtyArray[count++] = j;

				if (flag == VALUE_FLAG)
					dirtyArray[count++] = diceValues[j];

				if (flag == ABS_VALUE_FLAG)
					dirtyArray[count++] = Math.abs(diceValues[j]);
			}

		}

		if (count == 0)
			return null;

		int[] cleanArray = new int[count];
		System.arraycopy(dirtyArray, 0, cleanArray, 0, count);
		return cleanArray;
	}

	// Returns the index in dieValues[]
	public static int identifyView(View v) {
		int imgIndex;

		switch (v.getId()) {
		case (R.id.img_1):
			imgIndex = 0;
			break;
		case (R.id.img_2):
			imgIndex = 1;
			break;
		case (R.id.img_3):
			imgIndex = 2;
			break;
		case (R.id.img_4):
			imgIndex = 3;
			break;
		case (R.id.img_5):
			imgIndex = 4;
			break;
		case (R.id.img_6):
			imgIndex = 5;
			break;
		default:
			Log.w("DieManager.IdentifyView", "Could not identify view");
			return -1;
		}

		return imgIndex;
	}

	public Integer getImage(int index) {
		if (diceValues[index] == 0) {
			return letterImages.get(index);

		} else {
			return diceImages.get((diceValues[index]));
		}
	}
}
